﻿using UnityEngine;
using System.Collections;

namespace Lost{
	/// <summary>
	/// 背包交互=背包管理=背包UI
	/// </summary>
	public class BackpackInteraction : MonoBehaviour {
		public static BackpackInteraction _instance;
		///背包
		[SerializeField] LostBackpack bag = new LostBackpack();
		///收集捡到的ItemTag的物品（位置）
		GameObject ITCollector;

		///物品拿出后的位置
		Transform HoldingPos{
			get{ 
				return RightPos;
			}
		}
		[SerializeField] string leftHoldingPosName;
		Transform leftHoldingPos;
		Transform LeftPos{
			get{ 
				if (leftHoldingPos == null) {
					leftHoldingPos = ItemsManager.FindTransform (leftHoldingPosName);
				}
				return leftHoldingPos;
			}
		}
		[SerializeField] string rightHoldingPosName;
		Transform rightHoldingPos;
		Transform RightPos{
			get{ 
				if (rightHoldingPos == null) {
					rightHoldingPos = ItemsManager.FindTransform (rightHoldingPosName);
				}
				return rightHoldingPos;
			}
		}

		///当前要显示的物品索引，-1表示背包为空
		int CurIndex{
			get{ 
				int bagCount = bag.Count;
				if (bagCount <= 0) {
					curIndex = -1;
				} else if (curIndex < 0) {
					curIndex = 0;
				} else if (curIndex >= bagCount) { 
					curIndex = bagCount - 1;
				}
				return curIndex;
			}
			set{ 
				curIndex = value;
				curIndex = CurIndex;
			}
		}
		int curIndex = -1;
		///拿出的物品（默认为右手拿着的物品）
		public ToolItem HoldingItem{
			get{ 
				return holdingItem;
			}
		}
		ToolItem holdingItem;
		public Transform LeftItem{
			get{ 
				if (LeftPos != null) {
					return LeftPos.GetChild (0);
				}
				return null;
			}
		}
		public Transform RightItem{
			get{ 
				if (RightPos != null) {
					return RightPos.GetChild (0);
				}
				return null;
			}
		}

		///当前显示在UI上的物品
		public static GameObject ItemHolding{
			get{ return _instance.itemShowing;}
		}
		GameObject itemShowing;
		///UI显示物品的位置
		[SerializeField] Transform showPos;
		///文字显示控制脚本
		[SerializeField] TextShower textShower;

		void Awake () {
			_instance = this;
			ITCollector = new GameObject ("ItemTags Collector");
			ITCollector.transform.parent = transform;
		}
		void Update(){
			if (Input.GetKeyDown (KeyCode.LeftArrow)) {
				PreItem ();
			}
			if (Input.GetKeyDown (KeyCode.RightArrow)) {
				NextItem ();
			}
			if (Input.GetKeyDown (KeyCode.Space)) {
				TakeOut ();
			}
		}

		///捡起
		public static void PickUp(ToolItem ti){
			_instance.bag.Add (ti.CopyTo (_instance.ITCollector));
			ItemsManager.iDestroy (ti.gameObject);
			_instance.CurIndex = _instance.bag.FindItemIndex (ti.ItemName);
			_instance.ShowCurItem ();
		}

		///拿出当前显示的物品并放在默认的手上（右手）
		public void TakeOut(){
			if (itemShowing != null) {
				itemShowing.transform.position = HoldingPos.position;
				itemShowing.transform.parent = HoldingPos;
				itemShowing.transform.localScale /= itemShowing.GetComponent<ToolItem> ().ShowScale;
			}
		}

		///使用当前拿出的物品
		public static void UseCurItem(){
			if (_instance.CurIndex != -1) {
				_instance.bag.RemoveAt (_instance.curIndex);
				_instance.ShowCurItem ();
			}
		}
		///使用当前名为itemName的物品
		public static void UseItem(string itemName){
			if (_instance.bag.Contains (itemName)) {
				_instance.bag.Remove (itemName);
				_instance.ShowCurItem ();
			}
		}

		///生成当前的物品显示在UI上
		void ShowCurItem(){
			if (itemShowing != null) {
				Destroy (itemShowing);
			}
			if (CurIndex != -1) {
				ToolItem ti = bag [curIndex].toolItem;
				GameObject prefab = ti.Prefab;
				itemShowing = Instantiate (prefab, showPos.position, showPos.rotation) as GameObject;
				Transform itemTr = itemShowing.transform;
				itemTr.localScale *= ti.ShowScale;
				itemTr.parent = showPos;
				itemShowing.GetComponent<ToolItem> ().SetInteractive (false);

				textShower.SetContent (ti.Introduction);
			} else {
				itemShowing = new GameObject ();
				itemShowing.transform.parent = showPos;
				textShower.SetContent ("");
			}
		}
		///上一个物品
		public void PreItem(){
			int lastValue = curIndex;
			CurIndex--;
			if (lastValue != curIndex) {
				ShowCurItem ();
			}
		}
		///下一个物品
		public void NextItem(){
			int lastValue = curIndex;
			CurIndex++;
			if (lastValue != curIndex) {
				ShowCurItem ();
			}
		}
	}
}
